package com.zhixin.fans.ks.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;

import com.zhixin.fans.ks.R;


public class BottomRecySheetBuilder<T> implements View.OnClickListener {
    private Context mContext;

    private QMUIBottomSheet mDialog;

    private BaseQuickAdapter<T, BaseViewHolder> mAdapter;

    private RecyclerView mContainerView;
    RecyclerView.LayoutManager mLayoutManager;

    private String mTitle;
    private TextView mTitleTv;

    private String mBottomTitle;
    private TextView mBottomTitleTv;


    private DialogInterface.OnDismissListener mOnBottomDialogDismissListener;


    public BottomRecySheetBuilder(Context context, RecyclerView.LayoutManager layoutManager, BaseQuickAdapter<T, BaseViewHolder> adapter) {
        mContext = context;
        mLayoutManager = layoutManager;
        mAdapter = adapter;
    }


    public BottomRecySheetBuilder addItem(T t) {
        mAdapter.getData().add(t);
        return this;
    }


    public BottomRecySheetBuilder setOnSheetItemClickListener(BaseQuickAdapter.OnItemClickListener listener) {
        mAdapter.setOnItemClickListener(listener);
        return this;
    }

    public BottomRecySheetBuilder setOnBottomDialogDismissListener(DialogInterface.OnDismissListener listener) {
        mOnBottomDialogDismissListener = listener;
        return this;
    }


    public BottomRecySheetBuilder setTitle(String title) {
        mTitle = title;
        return this;
    }

    public BottomRecySheetBuilder setBottomTitle(String title) {
        mBottomTitle = title;
        return this;
    }

    public BottomRecySheetBuilder setTitle(int resId) {
        mTitle = mContext.getResources().getString(resId);
        return this;
    }

    public QMUIBottomSheet build() {
        mDialog = new QMUIBottomSheet(mContext);
        View contentView = buildViews();
        mDialog.setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        if (mOnBottomDialogDismissListener != null) {
            mDialog.setOnDismissListener(mOnBottomDialogDismissListener);
        }
        return mDialog;
    }

    private View buildViews() {
        View wrapperView = View.inflate(mContext, getContentViewLayoutId(), null);
        mTitleTv = wrapperView.findViewById(R.id.title);
        mBottomTitleTv = wrapperView.findViewById(R.id.tv_bottom_title);

        mContainerView = wrapperView.findViewById(R.id.listview);
        if (!TextUtils.isEmpty(mTitle)) {
            mTitleTv.setVisibility(View.VISIBLE);
            mTitleTv.setText(mTitle);
        } else {
            mTitleTv.setVisibility(View.GONE);
        }


        if (!TextUtils.isEmpty(mBottomTitle)) {
            mBottomTitleTv.setVisibility(View.VISIBLE);
            mBottomTitleTv.setText(mBottomTitle);
        } else {
            mBottomTitleTv.setVisibility(View.GONE);
        }

        mBottomTitleTv.setOnClickListener(this);


        mContainerView.setLayoutManager(mLayoutManager);
        mContainerView.setAdapter(mAdapter);
        return wrapperView;
    }


    protected int getListMaxHeight() {
        return (int) (QMUIDisplayHelper.getScreenHeight(mContext) * 0.5);
    }

    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    protected int getContentViewLayoutId() {
        return R.layout.dialog_bottom_recy_layout;
    }


    @Override
    public void onClick(View view) {
        mDialog.dismiss();
    }
}
